#coding=utf8

from util.configer_generator import configer
from appium import webdriver
from util.path_generator import filePath
import os
import re



class Driver:
    def __init__(self,device_name,chromedrive_dirname,device_section):
        self.device_name = device_name
        flag = configer.get_str("if_install_auto", "flag")
        if flag=="package":
            self.caps={
            "platformName":configer.get_str(device_section,"platformname"),
            "platformVersion": self.get_Android_Release().strip(),
            "deviceName": self.device_name,
            "appPackage": configer.get_str(device_section,"package"),
            "appActivity": configer.get_str(device_section,"appactivity"),
            "noReset": configer.get_boolean(device_section,"noreset"),
            "automationName":"uiautomator2",
            "udid": self.device_name,
            "chromedriverExecutable":filePath.path_chromedriver+chromedrive_dirname
        }
        elif flag=="apk":
            #通过安装包自动获取包名和launch_activity
            app_path=filePath.path_apk+configer.get_str(device_section,"apk_name")
            package_temp = os.popen('aapt d badging %s | findstr "package"' % (app_path)).readline()
            appPackage = re.search(r"package: name='(.+?)' ",package_temp).group(1)
            package_temp = os.popen('aapt d badging %s | findstr "aunchable-activity"' % (app_path)).readline()
            appActivity = re.search(r"name='(.+?)'  ",package_temp).group(1)


            #apk的caps也必须加上appPackage,appActivity否则无法正常打开app
            self.caps = {
                "platformName": configer.get_str(device_section, "platformname"),
                "platformVersion": self.get_Android_Release().strip(),
                "deviceName": self.device_name,
                "app": app_path,
                "noReset": configer.get_boolean(device_section, "noreset"),
                "automationName":"uiautomator2",
                "udid": self.device_name,
                "appPackage": appPackage,
                "appActivity": appActivity,
                # "newCommandTimeout":60,
                "chromedriverExecutable": filePath.path_chromedriver + chromedrive_dirname
            }
            self.caps1= {
                "platformName": configer.get_str(device_section, "platformname"),
                "platformVersion": self.get_Android_Release().strip(),
                "deviceName": self.device_name,
                "appPackage": "io.appium.settings",
                "appActivity": "io.appium.settings.Settings",
                "noReset": configer.get_boolean(device_section, "noreset"),
                "automationName":"uiautomator2",
                "udid": self.device_name
            }




    def create_driver(self,p_port):
        self.driver=webdriver.Remote("http://127.0.0.1:"+str(p_port)+"/wd/hub",self.caps)
        # print(self.caps)
        # self.driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",self.caps)




    def get_Android_Release(self):
        temp = configer.get_str("devices", "get_release")
        commd_str=temp[:6]+" "+self.device_name+" "+temp[7:]
        version=os.popen(commd_str).readline()
        return version





